package com.example.poemtest.category;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.poemtest.Activity.BottomNavigationViewActivity;
import com.example.poemtest.R;
import com.example.poemtest.index.activity.StudyPoetryDetailActivity;
import com.example.poemtest.entity.DynastryPoetryBean;
import com.example.poemtest.index.entity.Poetry;
import com.example.poemtest.utils.Constant;
import com.example.poemtest.utils.FullScreenUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * 分朝代的诗词页面
 */
public class PoetryDynastyActivity extends AppCompatActivity {

    private ImageView imageView, back;
    private ListView listView;
    private PoetryDynastyAdapter poetryAdapter;
    private List<DynastryPoetryBean.DynastyPoetry> poetryList;
    private TextView dynastyText;
    private String dynasty;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FullScreenUtil.getInstance().fullScreen(PoetryDynastyActivity.this,true);
        setContentView(R.layout.activity_poetry_dynasty);

        //绑定控件对象
        setView();

        Intent intent = getIntent();
        dynastyText.setText(intent.getStringExtra("Dynasty"));
        dynasty = intent.getStringExtra("Dynasty").substring(0,2);
        Log.e("dy",dynasty);

        //设置数据源
        poetryList = setData(dynasty);
        //设置适配器
        poetryAdapter = new PoetryDynastyAdapter(
                this,
                Constant.poetryDynastyList,
                R.layout.layout_poetry_dynasty_item
        );
        //绑定适配器对象
        listView.setAdapter(poetryAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Poetry poetry = new Poetry();
//        Log.e("名字",dataList.get(position).getPoetry_name().toString());
                poetry.setPoetry_desc(poetryList.get(position).getTranslation());
                poetry.setPoetry_name(poetryList.get(position).getTitle());
                poetry.setPoetry_author(poetryList.get(position).getAutuor());
                poetry.setPoetry_author_dynasty(poetryList.get(position).getDynasty());
                poetry.setPoetry_content(poetryList.get(position).getContent());
                Log.e("译文",poetryList.get(position).getTranslation());
//        Log.e("1名字",poetry.getPoetry_name());
                Intent intent = new Intent(PoetryDynastyActivity.this, StudyPoetryDetailActivity.class);
                intent.putExtra("poetry", poetry);
//        Log.e("诗词",poetry.toString());
                startActivity(intent);
            }
        });

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(PoetryDynastyActivity.this, BottomNavigationViewActivity.class);
                intent1.putExtra("id",2);
                startActivity(intent1);
            }
        });
    }

    /**
     * 设置数据源
     * 改成数据库里的json文件
     */
    private List<DynastryPoetryBean.DynastyPoetry> setData(String dynasty) {
//        String json = getJson(PoetryDynastyActivity.this, "dynastypoetry.json");
//        DynastryPoetryBean dynastryPoetryBean = JSONObject.parseObject(json,DynastryPoetryBean.class);
        List<DynastryPoetryBean.DynastyPoetry> list = new ArrayList<>();
        List<DynastryPoetryBean.DynastyPoetry> list1 = Constant.poetryDynastyList;
        for (DynastryPoetryBean.DynastyPoetry dp : list1) {
            if (dp.getDynasty().equals(dynasty)) {
                Log.e("诗",dp.getTranslation());
                list.add(dp);
            }
        }
//        Log.e("诗",list.get(0).toString());
        return list;
    }

    /**
     * 得到json文件中的内容
     *
     * @param context
     * @param fileName
     * @return
     */
    public static String getJson(Context context, String fileName) {
        StringBuilder stringBuilder = new StringBuilder();
        //获得assets资源管理器
        AssetManager assetManager = context.getAssets();
        //使用IO流读取json文件内容
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName), "utf-8"));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

    /**
     * 绑定控件对象
     */
    private void setView() {
        imageView = findViewById(R.id.img_poetry);
        listView = findViewById(R.id.poetry_dynasty_list_view);
        dynastyText = findViewById(R.id.poetry_dynasty_name);
        back = findViewById(R.id.back);
    }

}